﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TheDrinksApp.Database;
using TheDrinksApp.Models;
namespace TheDrinksApp.Screen.CurrentOrder.TableViewCell
{
    public partial class CurrentOrderTableViewCell : UserControl
    {
        public CurrentOrderScreen parent;
        public int stt;
        public CurrentOrderDAO order;
        public CurrentOrderTableViewCell(CurrentOrderDAO _order, CurrentOrderScreen _parent, int _stt)
        {
            InitializeComponent();
            parent = _parent;
            
            order = _order;
            stt = _stt;

            
        }
        public void CreateText()
        {
            this.gridText.Children.Clear();
                AutoSizeTextView auto = new AutoSizeTextView();
                auto.order = this.order;
                this.gridText.Children.Add(auto);
        }
        private void Button_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //if (btnDelete.Visibility == System.Windows.Visibility.Visible)
            //{
            //    btnDelete.Visibility = System.Windows.Visibility.Collapsed;
            //    return;
            //}
            parent.GoToDetail(order);
        }
        private void OnFlick(object sender, FlickGestureEventArgs e)
        {
            if ((e.Angle >= 340 && e.Angle <= 360) || (e.Angle >= 0 && e.Angle <= 20) || (e.Angle >= 160 && e.Angle <= 200))
            {
                if (e.HorizontalVelocity < 0 || e.HorizontalVelocity > 0)
                {
                    Visibility prev = btnDelete.Visibility;
                    parent.hideAllDeleteButton();
                    toggleButton(prev);
                }
            }
        }

        private void toggleButton(Visibility prevState)
        {
            if (prevState == Visibility.Visible)
            {
                hideDeleteButton();
            }
            else
            {
                showDeleteButton();
            }
        }

        public void showDeleteButton()
        {
            btnDelete.Visibility = Visibility.Visible;
        }

        public void hideDeleteButton()
        {
            btnDelete.Visibility = Visibility.Collapsed;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            parent.DeleteAtIndex(stt);
        }

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            tbNumber.Text = order.quantity.ToString();
            CreateText();
        }

    }
}
